package com.ttmusic.fragments.maintab;

import java.util.ArrayList;
import java.util.List;

import com.ttmusic.activitys.MainActivity;
import com.ttmusic.activitys.R;
import com.ttmusic.core.MusicManager;
import com.ttmusic.dao.MusicInfoDao;
import com.ttmusic.entity.AbstractMusic;
import com.ttmusic.entity.ArtistInfo;
import com.ttmusic.entity.MusicInfo;
import com.ttmusic.utils.Utils;
import com.ttmusic.view.adapters.ArtistMusicAdapter;
import com.ttmusic.view.listeners.OnItemClickListener;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 某位歌手的歌曲列表
 * @author Administrator
 *
 */
public class MusicOfArtistFragment extends ListFragment implements OnClickListener {
	
	//当前fragment的布局
	private RelativeLayout musicOfArtistFragment;
	//当前展示的歌手
	private ArtistInfo artistInfo;
	//当前展示的歌手的歌曲列表
	private List<AbstractMusic> musicInfos = new ArrayList<AbstractMusic>();
	private MusicInfoDao musicInfoDao;
	private View view;
	private ListView musicOfArtist;
	private ArtistMusicAdapter adapter;
	//顶部歌手名、播放全部、多选、全选/取消全选
	private TextView musicOfArtist_artist, playAll_this_artistmusic, multiselect, btn_selectAll;
	//返回歌手列表
	private ImageView icon_back_toartists;
	
	

	public MusicOfArtistFragment() {
		this.musicInfoDao = MusicInfoDao.getInstance();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_music_of_artist, container, false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		musicOfArtistFragment = (RelativeLayout)view.findViewById(R.id.fragment_music_of_artist);
		//返回
		icon_back_toartists = (ImageView)view.findViewById(R.id.icon_back_toartists);
		icon_back_toartists.setOnClickListener(this);
		//歌手名
		musicOfArtist_artist = (TextView)view.findViewById(R.id.musicOfArtistActionBar_artist);
		musicOfArtist_artist.setText(artistInfo.getArtist());
		musicOfArtist_artist.setOnClickListener(this);
		//全选/取消全选
		btn_selectAll = (TextView)view.findViewById(R.id.btn_selectAll);
		btn_selectAll.setOnClickListener(this);
		//播放全部
		playAll_this_artistmusic = (TextView)view.findViewById(R.id.playAll_this_artistmusic);
		playAll_this_artistmusic.setText("播放全部(共" + musicInfos.size() + "首)");
		playAll_this_artistmusic.setOnClickListener(this);
		//多选按钮
		multiselect = (TextView)view.findViewById(R.id.musicOfArtistActionBar_multiselect);
		multiselect.setOnClickListener(this);
		
		musicOfArtist = this.getListView();
		adapter = new ArtistMusicAdapter(getContext(), musicInfos);
		MainActivity.adapterCurrentShow = adapter;
		musicOfArtist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2, long index) {
				//Toast.makeText(getContext(), "Item被点击了", Toast.LENGTH_SHORT).show();
				if(ArtistMusicAdapter.isMultiChecking) {
					CheckBox music_item_checkbox = (CheckBox)v.findViewById(R.id.music_item_checkbox);
					ImageView selected_view = (ImageView)v.findViewById(R.id.selected_view);
					if(music_item_checkbox.isChecked()) {
						MainActivity.checkedMusicInfos.remove(musicInfos.get((int)index));
						music_item_checkbox.setChecked(false);
						selected_view.setImageResource(R.drawable.icon_checkbox_unchecked);
					}else {
						MainActivity.checkedMusicInfos.add(musicInfos.get((int)index));
						music_item_checkbox.setChecked(true);
						selected_view.setImageResource(R.drawable.icon_checkbox_checked);
					}
					if(MainActivity.checkedMusicInfos.size() < musicInfos.size()) {
						btn_selectAll.setText("全选");
					}else {
						btn_selectAll.setText("取消全选");
					}
					musicOfArtist_artist.setText("   已选中" + MainActivity.checkedMusicInfos.size() + "项");
				}else if(null != musicInfos && musicInfos.size() > 0) {
					if(MusicManager.getPlayingList() != musicInfos) {
						//播放音乐时判断当前点击的音乐所在list是不是当前播放列表  若不是，则切换播放列表为当前fragment的musicInfoList
				        MusicManager.setPlayingList(musicInfos);
					}
			        MainActivity.adapterCurrentPlay = adapter;
					MainActivity.mManager.prepareAndPlay((int)index);
					adapter.notifyDataSetChanged();
				}
			}
		});
		musicOfArtist.setAdapter(adapter);
		//this.onHiddenChanged(false);
		MainActivity main = (MainActivity)this.getActivity();
		main.setTopAndBottomActionBarVisibility(View.GONE, View.VISIBLE);
		main.setBottomOperationActionbarVisibility(View.GONE);
		MainActivity.adapterCurrentShow = adapter;
		MainActivity.previous = MainActivity.artistsFragment;
	}
	
	/**
	 * 界面数据发生了改变
	 * @param artistInfo
	 */
	public void dataChanged(ArtistInfo artistInfo) {
		this.artistInfo = artistInfo;
		musicInfos = musicInfoDao.loadByArtistId(artistInfo.getArtistId());
		if(adapter == null)
			adapter = new ArtistMusicAdapter(getContext(), musicInfos);
		adapter.setList(musicInfos);
		adapter.notifyDataSetChanged();
		if(null != musicOfArtist_artist) 
			musicOfArtist_artist.setText(artistInfo.getArtist());
		if(null != playAll_this_artistmusic) 
			playAll_this_artistmusic.setText("播放全部(共" + musicInfos.size() + "首)");
	}
	
	

	
	@Override
	public void onHiddenChanged(boolean isHidden) {
		MainActivity main = (MainActivity)this.getActivity();
		if(!isHidden) {
			View item;
			CheckBox music_item_checkbox;
			ImageView selected_view;
			for(int i=0;i<musicOfArtist.getChildCount();i++) {
				item = musicOfArtist.getChildAt(i);
				music_item_checkbox = (CheckBox)item.findViewById(R.id.music_item_checkbox);
				selected_view = (ImageView)item.findViewById(R.id.selected_view);
				music_item_checkbox.setChecked(false);
				selected_view.setImageResource(R.drawable.icon_checkbox_checked);
			}
			MainActivity.checkedMusicInfos.clear();
			multiselect.setVisibility(View.VISIBLE);
			main.setTopAndBottomActionBarVisibility(View.GONE, View.VISIBLE);
			main.setBottomOperationActionbarVisibility(View.GONE);
			btn_selectAll.setText("全选");
			btn_selectAll.setVisibility(View.GONE);
			adapter.cancelMultiCheck();
			musicOfArtist_artist.setText(artistInfo.getArtist());
			
			MainActivity.adapterCurrentShow = adapter;
			MainActivity.previous = MainActivity.artistsFragment;
		}else {
			main.setTopAndBottomActionBarVisibility(View.VISIBLE, View.VISIBLE);
		}
		super.onHiddenChanged(isHidden);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			//返回歌手列表界面
			case R.id.musicOfArtistActionBar_artist:
			case R.id.icon_back_toartists:
				if(ArtistMusicAdapter.isMultiChecking) {
					this.onHiddenChanged(false);
				}else {
					Utils.go(getFragmentManager(), this, MainActivity.previous, R.id.framecontent);
				}
				break;
			//播放全部
			case R.id.playAll_this_artistmusic:
				if(null != musicInfos && musicInfos.size() > 0) {
					if(MusicManager.getPlayingList() != musicInfos) {
						//播放音乐时判断当前点击的音乐所在list是不是当前播放列表  若不是，则切换播放列表为当前fragment的musicInfoList
				        MusicManager.setPlayingList(musicInfos);
				        MainActivity.adapterCurrentPlay = adapter;
					}
					MainActivity.mManager.prepareAndPlay(0);
				}
				break;
			//开始多选
			case R.id.musicOfArtistActionBar_multiselect:
				MainActivity.checkedMusicInfos.clear();
				multiselect.setVisibility(View.GONE);
				MainActivity main = (MainActivity)this.getActivity();
				main.setTopAndBottomActionBarVisibility(View.GONE, View.GONE);
				main.setBottomOperationActionbarVisibility(View.VISIBLE);
				btn_selectAll.setVisibility(View.VISIBLE);
				musicOfArtist_artist.setText("   已选中0项");
				adapter.beginMultiCheck();
				break;
			//全选/取消全选
			case R.id.btn_selectAll:
				View item;
				CheckBox music_item_checkbox;
				ImageView selected_view;
				if("全选".equals(btn_selectAll.getText())) {
					for(int i=0;i<musicInfos.size();i++) {
						item = musicOfArtist.getChildAt(i);
						music_item_checkbox = (CheckBox)item.findViewById(R.id.music_item_checkbox);
						selected_view = (ImageView)item.findViewById(R.id.selected_view);
						music_item_checkbox.setChecked(true);
						selected_view.setImageResource(R.drawable.icon_checkbox_checked);
					}
					MainActivity.checkedMusicInfos.clear();
					MainActivity.checkedMusicInfos.addAll(musicInfos);
					btn_selectAll.setText("取消全选");
				}else {
					for(int i=0;i<musicInfos.size();i++) {
						item = musicOfArtist.getChildAt(i);
						music_item_checkbox = (CheckBox)item.findViewById(R.id.music_item_checkbox);
						selected_view = (ImageView)item.findViewById(R.id.selected_view);
						music_item_checkbox.setChecked(false);
						selected_view.setImageResource(R.drawable.icon_checkbox_unchecked);
					}
					MainActivity.checkedMusicInfos.clear();
					btn_selectAll.setText("全选");
				}
				musicOfArtist_artist.setText("   已选中" + MainActivity.checkedMusicInfos.size() + "项");
				break;
			default:
				break;
		}
		
	}
	
	
}
